﻿
#region Using

using System;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities.Connectors;
using Rio.Framework.DataAccess;
using NetBay.Core.Log;
using Rio.Manager.Business.Repository.Translator;
using NetBay.Core.BusinessEntities;

#endregion Using

namespace Rio.Framework.Business.Repository
{
    public class ConnectorRepository
    {
        #region Selection
        
        /// <summary>Get connector name.</summary>
        /// <returns>Connector name.</returns>
        public static List<string> GetConnectorNameList()
        {
            //Variable declaration.
            List<string> l_oConnectorListName = new List<string>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorRepository.GetConnectorNameList --> No parameters");

            try
            {                
                l_oConnectorListName = ConnectorTranslator.GetConnectorNameList(DA_Connector.GetConnectorNameList());
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("ConnectorRepository.GetConnectorNameList Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_oConnectorListName;
        }

        /// <summary>Get connector information list.</summary>
        /// <returns>Connector infomation list.</returns>
        public static List<Connector> GetConnectorInformationList()
        {
            //Variable declaration.
            List<Connector> l_oConnectorInformationList = new List<Connector>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorRepository.GetConnectorInformationList --> No parameters");                

            try
            {
                l_oConnectorInformationList = ConnectorTranslator.GetConnectorInformationList(DA_Connector.GetConnectorInformationList());
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("ConnectorRepository.GetConnectorInformationList Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_oConnectorInformationList;
        }

        /// <summary>Get folder type indexes correspondence list.</summary>
        /// <param name="p_sFolderTypeTableName">Folder type table name.</param> 
        /// <returns>Folder type indexes correspondence list.</returns>
        public static List<ConnectorCorrespondence> GetFolderTypeIndexCorrespondanceList(string p_sFolderTypeTableName)
        {
            //Variable declaration.
            List<ConnectorCorrespondence> l_oFolderTypeIndexCorrespondanceList = new List<ConnectorCorrespondence>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorRepository.GetFolderTypeIndexCorrespondanceList (p_sFolderTypeTableName <" + p_sFolderTypeTableName + ">)");
            
            try
            {                
                l_oFolderTypeIndexCorrespondanceList = ConnectorTranslator.GetFolderTypeIndexCorrespondanceList(DA_Connector.GetFolderTypeIndexCorrespondanceList(p_sFolderTypeTableName));
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("ConnectorRepository.GetFolderTypeIndexCorrespondanceList Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_oFolderTypeIndexCorrespondanceList;
        }

        #endregion Selection

        #region Added

        /// <summary>Add RIO connector.</summary>
        /// <param name="p_sConnectorName">Connector name to add.</param>
        /// <param name="p_sConnectorDescription">Connector description to add.</param>
        /// <returns>Connector Identifiant. (recon_id)</returns>
        public static ReturnLong AddConnector(string p_sConnectorName, string p_sConnectorDescription)
        {
            //Variable declaration.
            var l_lConnectorId = new ReturnLong();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorRepository.AddConnector (p_sConnectorName <" + p_sConnectorName + ">, p_sConnectorDescription <" + p_sConnectorDescription + ">)");
                        
            try
            {
                l_lConnectorId.Value  = DA_Connector.AddConnector(p_sConnectorName, p_sConnectorDescription); 
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorRepository.AddConnector Exception [" + ex.Message + "]");
                l_lConnectorId = new ReturnLong("(p_sConnectorName <" + p_sConnectorName + ">, p_sConnectorDescription <" + p_sConnectorDescription + ">)", ex);
            }

            //Return value.
            return l_lConnectorId;
        }

        /// <summary>Add RIO correspondence connector.</summary>
        /// <param name="p_oConnectorCorrespondenceToAdd">Connector Correspondence Object.</param>
        /// <param name="p_lConnectorId">Connector Id.</param>
        /// <param name="p_lEntityId">Entity Id.</param>
        /// <param name="p_lFolderTypeId">Folder Type Id.</param>        
        public static void AddCorrespondenceConnector(List<ConnectorCorrespondence> p_oConnectorCorrespondenceToAdd ,long p_lConnectorId, long p_lEntityId, long p_lFolderTypeId)
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorRepository.AddCorrespondenceConnector (p_lEntityId <" + p_lEntityId + ">, p_lFolderTypeId <" + p_lFolderTypeId + ">, p_lConnectorId <" + p_lConnectorId + ">, Nombre de correspondances du connecteur: p_oConnectorCorrespondenceToAdd.Count() <" + p_oConnectorCorrespondenceToAdd.Count + ">)");
           
            try
            {
                DA_Connector.AddCorrespondenceConnector(p_oConnectorCorrespondenceToAdd, p_lConnectorId, p_lEntityId, p_lFolderTypeId);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorRepository.AddCorrespondenceConnector Exception [" + ex.Message + "]");
            }
        }

        /// <summary>Add RIO connector parameters.</summary>
        /// <param name="p_oConnectorParametersToAdd">Connector parameters Object.</param>
        /// <param name="p_lConnectorId">Connector Id.</param>
        public static void AddParameterConnector(List<ConnectorParameters> p_oConnectorParametersToAdd, long p_lConnectorId)
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorRepository.AddParameterConnector (p_lConnectorId <" + p_lConnectorId + ">, Nombre de paramètres du connecteur: p_oConnectorParametersToAdd.Count() <" + p_oConnectorParametersToAdd.Count + ">)");

            try
            {
                DA_Connector.AddParameterConnector(p_oConnectorParametersToAdd, p_lConnectorId);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorRepository.AddParameterConnector Exception [" + ex.Message + "]");
            }
        }

        #endregion Added

        #region Delete

        /// <summary>Delete Rio connector.</summary>
        /// <param name="p_lConnectorId">Connector Id.</param> 
        public static void DeleteConnectorByConnectorId(long p_lConnectorId)
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorRepository.DeleteConnectorByConnectorId (p_lConnectorId <" + p_lConnectorId + ">)");

            try
            {
                DA_Connector.DeleteConnectorByConnectorId(p_lConnectorId);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorRepository.DeleteConnectorByConnectorId Exception [" + ex.Message + "]");
            }
        }

        /// <summary>Delete list Rio connector.</summary>
        /// <param name="p_sConnectorListId)">List Connector Id.</param> 
        public static void DeleteConnectorByListConnectorId(string p_sConnectorListId)
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorRepository.DeleteConnectorByListConnectorId (p_sConnectorListId <" + p_sConnectorListId + ">)");
            
            try
            {
                DA_Connector.DeleteConnectorByListConnectorId(p_sConnectorListId);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorRepository.DeleteConnectorByListConnectorId Exception [" + ex.Message + "]");
            }
        }

        #endregion Delete

        #region Update

        /// <summary>Update RIO connector.</summary>
        /// <param name="p_lConnectorId">Connector Id.</param>   
        /// <param name="p_sConnectorDescription">Connector description.</param>
        /// <param name="p_sConnectorName">Connector name.</param>
        public static ReturnLong UpdateNameAndDescriptionConnectorByConnectorId(long p_lConnectorId, string p_sConnectorName, string p_sConnectorDescription)
        {
            //Variable declaration.
            var l_lConnectorId = new ReturnLong();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("ConnectorRepository.UpdateNameAndDescriptionConnectorByConnectorId (p_lConnectorId <" + p_lConnectorId + ">, p_sConnectorName <" + p_sConnectorName + ">, p_sConnectorDescription <" + p_sConnectorDescription + ">)");

            try
            {
                l_lConnectorId.Value = DA_Connector.UpdateNameAndDescriptionConnectorByConnectorId(p_lConnectorId, p_sConnectorName, p_sConnectorDescription);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("ConnectorRepository.UpdateNameAndDescriptionConnectorByConnectorId Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_lConnectorId;
        }

        #endregion Update
    }
}
